package com.study.practice.collect;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
    @Override
    public Supplier<List<T>> supplier() {
        return ArrayList::new;
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        return List::add;
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (list1, list2) -> {
            list1.addAll(list2);
            return list1;
        };
    }

    @Override
    public Function<List<T>, List<T>> finisher() {
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
    }


    @Data
    static class Insurance {
        private String name;
    }

    @Data
    static class Car {
        private Optional<Insurance> insurance;
    }

    @Data
    static class Person {
        private Optional<Car> car;
        private Optional<Insurance> insurance;
    }

    public static void main(String[] args) {
       List<String> shops = Arrays.asList(args);
        final List<CompletableFuture<String>> completableFutures = shops.stream()
                .map(shop -> CompletableFuture.supplyAsync(() -> String.format("%s price is %.2f", "aa", 16)))
                .collect(Collectors.toList());

        final List<String> collect = completableFutures.stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());

        completableFutures.stream().map(future -> {
            try {
                return future.get();
            } catch (Exception e) {
                System.out.println(e);
            }
            return null;
        }).collect(Collectors.toList());

    }

}
